package edu.rutgers.moko.data;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.TimerTask;

import org.apache.commons.io.FileUtils;

import com.sun.syndication.feed.synd.SyndContent;
import com.sun.syndication.feed.synd.SyndContentImpl;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndEntryImpl;

import edu.rutgers.moko.data.util.FieldAccessUtil;
import edu.rutgers.moko.data.util.LRUCache;
import edu.rutgers.moko.data.util.OMField;


/**
 * 
 * @author Darja Krushevskaja
 *
 */
public class MyTask extends TimerTask {

	private LRUCache<Date, FeedData> lru;
	private FeedData prev = null;

	public MyTask(int max) {
		this.lru = new LRUCache<Date, FeedData>(max);
	}

	public LRUCache<Date, FeedData> getCache() {
		return lru;
	}

	@SuppressWarnings("unchecked")
	public void run() {
		 Date date = new Date();
		// collect the data
		FeedData item = new FeedData();
		
		try {
			item.setDate(date);
			// batteryStatus
			File file = new File("/sys/devices/platform/bq27000-battery.0/power_supply/bat/status");
			List<String> lines = FileUtils.readLines(file);
			if (lines.size() == 1) {
				item.setBatteryStatus(lines.get(0));
			} else {
				item.setBatteryStatus("unknown");
			}

			// batteryCapacity
			file = new File("/sys/devices/platform/bq27000-battery.0/power_supply/bat/capacity");
			lines = FileUtils.readLines(file);
			if (lines.size() == 1) {
				item.setBatteryCapacity(lines.get(0));
			} else {
				item.setBatteryCapacity(null);
			}
			
			// timeTiEmpty
			file = new File("/sys/devices/platform/bq27000-battery.0/power_supply/bat/time_to_empty_now");
			lines = FileUtils.readLines(file);
			if (lines.size() == 1) {
				item.setTimeToEmpty(lines.get(0));
			} else {
				item.setTimeToEmpty(null);
			}
			

			// currentBrightness
			file = new File("/sys/devices/platform/s3c2440-i2c/i2c-adapter/i2c-0/0-0073/backlight/pcf50633-bl/actual_brightness");
			lines = FileUtils.readLines(file);
			if (lines.size() == 1) {
				item.setCurrentBrightness(lines.get(0));
			} else {
				item.setCurrentBrightness(null);
			}

			// timezone;
			file = new File("/etc/timezone");
			lines = FileUtils.readLines(file);
			if (lines.size() == 1) {
				item.setTimezone(lines.get(0));
			} else {
				item.setTimezone("unknown");
			}

			// bluetooth;
			file = new File("/sys/bus/platform/devices/neo1973-pm-bt.0/power_on");
			lines = FileUtils.readLines(file);
			if (lines.size() == 1) {
				item.setBluetooth(lines.get(0));
			} else {
				item.setBluetooth("unknown");
			}

		} catch (IOException e) {
			System.out.println("Error, cannot create new item!");
			throw new RuntimeException(e);
		}
		if (prev == null || !prev.equals(item)) {
			lru.put(date, item);
			prev = item;
		}
	
	}
	
	public List<SyndEntry> getFeeds(String ip, FieldAccessUtil faUtil){
		LinkedList<SyndEntry> entries = new LinkedList<SyndEntry>();
		Collection<FeedData> s = lru.values();
		SyndEntry entry;
		SyndContent description;
		FeedData previous = new FeedData();
		
		
		for (FeedData key : s) {
			StringBuilder desc = new StringBuilder();

			if(key.batteryStatusChanged(previous) && faUtil.isVisible(ip, OMField.BATTERY_STATUS)){
				desc.append("<b>Battery status: </b>").append(key.getBatteryStatus()).append("<br/>");
			}
			if(key.batteryCapacityChanged(previous)&& faUtil.isVisible(ip, OMField.BATTERY_CAPACITY)){
				desc.append("<b>Battery capacity: </b>").append(key.getBatteryCapacity()).append("<br/>");
			}
			if(key.timeToEmptyChanged(previous)&& faUtil.isVisible(ip, OMField.BATTERY_TTE)){
				desc.append("<b>Battery time to empty: </b>").append(key.getTimeToEmpty()).append("<br/>");
			}
			if(key.currentBrightnessChanged(previous) && faUtil.isVisible(ip, OMField.BRIGHTNESS_CURR)){
				desc.append("<b>Current brightness of the phone: </b>").append(key.getCurrentBrightness()).append("<br/>");
			}
			if(key.timezoneChanged(previous) && faUtil.isVisible(ip, OMField.TIMEZONE)){
				desc.append("<b>Timezone: </b>").append(key.getTimezone()).append("<br/>");
			}
			if(key.bluetoothChanged(previous) && faUtil.isVisible(ip, OMField.BLUETOOTH)){
				desc.append("<b>Bluetooth is: </b>").append(key.getBluetooth()).append("<br/>");
			}
			
			if(desc.length()>0){
				entry = new SyndEntryImpl();
				entry.setTitle("Openmoko state updates at " + key.getDate());
				entry.setLink("/rss");
				entry.setPublishedDate(key.getDate());
				description = new SyndContentImpl();
				description.setType("text/html");
				description.setValue(desc.toString());
				entry.setDescription(description);
				previous = key;
				entries.addFirst(entry);
			}
		}
		return entries;
	}
}
